// 路由
var express = require('express');
var router  = express.Router();

var queryArray  = require('../lib/queryArray');
var queryObject = require('../lib/queryObject');
var _ = require("underscore")._;
router.maxSockets = 200;

/*
router.post('*',function (req,res,next) {
    res.setHeader("Access-Control-Allow-Origin","*");
    res.setHeader("Access-Control-Allow-Methods","POST");
    res.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type");
    next();
});
*/

router.post('/api/login',function(req,res,next){
    var param    = req.body;
    var username = param.username;
    var psw      = param.psw;

    var db       = require('../lib/db.js');

    db.execQuery({
        sql : 'select * from usr where usr_num = ?' ,
        args: [username],
        handler: function (result){
			if ( !result || result.length === 0 ) {
				console.log(result);
				// 没有该学号的学生
				res.setHeader('content-type', 'text/plain;charset=utf-8');
				res.json( {status : 2,msg : '没有找到该学生，请确认您的学号没有问题'} );
				res.end();
			}else{
				var user = result[0];
				if( user.usr_psw !== psw ) {
					res.setHeader('content-type', 'text/plain;charset=utf-8');
					res.json({status:3,msg : '密码错误，请重新输入'});
					res.end();
				}else{
					res.setHeader('content-type', 'text/plain;charset=utf-8');
					res.json({status:1,msg : user});
					res.end();
					// /^[A-Za-z0-9_\u554A-\u9C52]+$/;
				}
			}
		}
    });
});



module.exports = router;